﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace DrawPolygonAndSquare
{
	public partial class MainGUI : Form
	{
		/// <summary>
		/// 当前绘制的点索引
		/// </summary>
		private int index = 0;

		/// <summary>
		/// 当前绘制多边形的边数
		/// </summary>
		private int borderCountValue = 0;

		/// <summary>
		/// 当前是否正在绘制
		/// </summary>
		private bool drawing = false;

		/// <summary>
		/// 记录当前绘制的点
		/// </summary>
		private List<Point> points = new List<Point>();

		/// <summary>
		/// 绘制线段的笔实例
		/// </summary>
		private Pen linePen = new Pen(Color.Blue, 1.5f);

		/// <summary>
		/// 绘制点的笔刷实例
		/// </summary>
		private Brush pointBrush = new SolidBrush(Color.Red);

		/// <summary>
		/// 本窗体画布
		/// </summary>
		private Graphics graphics;

		public MainGUI()
		{
			InitializeComponent();
			graphics = showArea.CreateGraphics();
			graphics.CompositingQuality = CompositingQuality.HighQuality;
			graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
			graphics.SmoothingMode = SmoothingMode.HighQuality;
		}

		/// <summary>
		/// 在当前画布上画一个点
		/// </summary>
		/// <param name="point">传入点对象</param>
		private void drawPoint(Point point)
		{
			// 点的直径
			int sideLength = 4;
			// 绘制点中心
			Point center = new Point(point.X - sideLength / 2, point.Y - sideLength / 2);
			Rectangle rectangle = new Rectangle(center, new Size(sideLength, sideLength));
			// 绘制圆点
			graphics.FillEllipse(pointBrush, rectangle);
		}

		/// <summary>
		/// 在当前画布上画一条线
		/// </summary>
		/// <param name="lineSegment">线段</param>
		private void drawLine(LineSegment lineSegment)
		{
			graphics.DrawLine(linePen, lineSegment.Start, lineSegment.End);
		}

		/// <summary>
		/// 计算当前画布上多边形面积
		/// </summary>
		/// <returns>多边形面积</returns>
		private double getSquare()
		{
			double total = 0;
			Point origin = new Point(0, 0);
			LineSegment currentLine = new LineSegment();
			LineSegment nextLine = new LineSegment();
			currentLine.Start = origin;
			nextLine.Start = origin;
			for (int i = 0; i < points.Count; i++)
			{
				currentLine.End = points[i];
				if (i == points.Count - 1)
				{
					nextLine.End = points[0];
				}
				else
				{
					nextLine.End = points[i + 1];
				}
				// 执行叉乘
				total = total + new Vector(currentLine).CrossProduct(new Vector(nextLine));
			}
			return Math.Abs(total / 2);
		}

		private void ok_Click(object sender, EventArgs e)
		{
			// 查错
			if (borderCountBox.Text.Equals(""))
			{
				MessageBox.Show("请输入多边形边数！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			try
			{
				borderCountValue = int.Parse(borderCountBox.Text);
			}
			catch (Exception)
			{
				MessageBox.Show("请输入有效整数！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			if (borderCountValue < 3)
			{
				MessageBox.Show("多边形边数不小于3！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			// 开始绘制
			showArea.Refresh();
			points.Clear();
			drawing = true;
			ok.Enabled = false;
		}

		private void showArea_MouseClick(object sender, MouseEventArgs e)
		{
			if (drawing)
			{
				// 绘制点
				Point point = new Point(e.X, e.Y);
				points.Add(point);
				drawPoint(point);
				// 当画了两个点以上，就和上一个点相连
				if (index > 0)
				{
					LineSegment segment = new LineSegment(points[index - 1], points[index]);
					drawLine(segment);
				}
				// 如果当前画的是最后一个点，则和第一个点也连上并结束绘制，计算面积
				if (index == borderCountValue - 1)
				{
					LineSegment segment = new LineSegment(points[index], points[0]);
					drawLine(segment);
					index = 0;
					ok.Enabled = true;
					drawing = false;
					// 计算面积并显示
					resultLabel.Text = "多边形面积：" + getSquare();
					return;
				}
				index++;
			}
		}
	}
}